package com.mate.cloud.lottery.config;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * MybatisPlus-Generator自定义模版生成CRUD、DTO、VO、Convert等
 * https://blog.51cto.com/wnhyang/10095513
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/1/24 10:14
 * @updateUser: MI
 * @updateTime: 2025/1/24 10:14
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class MybatisPlusGeneratorConfig {
    /**
     * 数据源
     */
    private static final String DATASOURCE_URL = "jdbc:mysql://192.168.110.106:3306/learn_mybatis?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";

    /**
     * 用户名
     */
    private static final String USER_NAME = "yxw01";

    /**
     * 密码
     */
    private static final String PASS_WORD = "yxw@123!";

    private static final String MODULE_NAME = "";

    private static final String OUTPUT_PATH = System.getProperty("user.dir");

    /**
     * 父包名
     */
    private static final String PARENT_PATH = "cn.wnhyang.generator";

    /**
     * 作者
     */
    private static final String AUTHOR = "wnhyang";

    /**
     * 表名
     */
    private static final List<String> TABLES = new ArrayList<>(Arrays.asList("lottery", "lottery_item"));

    public static void main(String[] args) {
        // 1、代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        // 2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");

        // 代码生成的地址： D:/Stu/src/main/java
        System.out.println(projectPath + "/spring-boot-lottery-redis/src/main/java");

        globalConfig.setOutputDir(projectPath + "/spring-boot-lottery-redis/src/main/java");
        globalConfig.setAuthor("MI"); //作者信息
        //是否覆盖原始生成的文件，选择true后，生成时若原来已有相同文件会自动覆盖，谨慎操作
        globalConfig.setFileOverride(true);
        globalConfig.setOpen(false);

        //实体属性 Swagger2 注解
//        globalConfig.setSwagger2(true);
        autoGenerator.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl(DATASOURCE_URL);
        dsc.setUsername(USER_NAME);
        dsc.setPassword(PASS_WORD);
        autoGenerator.setDataSource(dsc);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.learn.lottery.redis");
        packageConfig.setEntity("domain");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        autoGenerator.setPackageInfo(packageConfig);


        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);

        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        //设置是否启用Lombok
        strategy.setEntityLombokModel(true);
        //数据库表的前缀
        //strategy.setTablePrefix("tb_");
        autoGenerator.setStrategy(strategy);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        //执行生成操作
        autoGenerator.execute();
    }

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
}
